package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.BillInRecord;
import com.deer.wms.base.system.model.BillInRecordCriteria;
import com.deer.wms.base.system.model.BillInRecordDto;
import com.deer.wms.base.system.model.bill.BillInDetailStreamVo;
import com.deer.wms.base.system.service.BillInRecordService;
import com.deer.wms.common.annotation.Log;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import com.deer.wms.common.enums.BusinessType;
import com.deer.wms.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
* Created by  on 2019/10/18.
*/
@Controller
@RequestMapping("/billInRecord")
public class BillInRecordController  extends BaseController{

    private String prefix = "manage/inRecord";

    @Autowired
    private BillInRecordService billInRecordService;



    /**
    * 详情
    */
    @GetMapping("/detail")
    public String detail()
    {
        return prefix + "/detail";
    }

    @RequiresPermissions("billInRecord:view")
    @GetMapping()
    public String billInRecord()
    {
        return prefix + "/inRecord";
    }

    /**
    * 修改入库记录
    */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
    BillInRecord billInRecord = billInRecordService.findInRecordById(id);
        mmap.put("billInRecord", billInRecord);
        return prefix + "/edit";
    }

    /**
     * 保存修改记录
     */
    @RequiresPermissions("in:inRecord:edit")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave( BillInRecord billInRecord)
    {
        return toAjax(billInRecordService.updateBillInRecord(billInRecord));
    }


    /**
    * 新增
    */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存入库记录
     */
    @RequiresPermissions("in:inRecord:add")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BillInRecord billInRecord)
    {
        return toAjax(billInRecordService.insertBillInRecord(billInRecord));
    }

    /**
     * 删除入库记录
     */
    @RequiresPermissions("in:inRecord:remove")
    @Log(title = "组盘", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(billInRecordService.deleteBillInRecordByIds(ids));
    }


    @PostMapping("/insert")
    @ResponseBody
    public Result add(@RequestBody BillInRecord billInRecord) {
        billInRecordService.save(billInRecord);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id) {
        billInRecordService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody BillInRecord billInRecord) {
        billInRecordService.update(billInRecord);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    @ResponseBody
    public Result detail(@PathVariable Integer id) {
        BillInRecord billInRecord = billInRecordService.findById(id);
        return ResultGenerator.genSuccessResult(billInRecord);
    }

    @PostMapping("/list")
    @ResponseBody
    public  TableDataInfo list(BillInRecordCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<BillInRecord> list = billInRecordService.findAll();
        return getDataTable(list);
    }

    //@RequiresPermissions("in:inRecord:list")
    @PostMapping("/findList")
    @ResponseBody
    public  TableDataInfo findList(BillInDetailStreamVo billInDetailStreamVo) {
        startPage();
        List<BillInDetailStreamVo> list = billInRecordService.findStreamList(billInDetailStreamVo);
        return getDataTable(list);
    }
    //打印
    @RequestMapping("print")
    public String print(@RequestParam String ids, Model model) {
        List<BillInRecordDto> byIds = new ArrayList();
        String[] split = ids.split(",");
        for (String s : split
        ) {
            BillInRecordCriteria b = new BillInRecordCriteria();

            b.setBillInRecordId(Integer.parseInt(s));
            List<BillInRecordDto> list = billInRecordService.findListbyid(b);
            for (BillInRecordDto bs : list
            ) {
                byIds.add(bs);
            }
        }
        model.addAttribute("byids", byIds);

        return prefix + "/print";
    }

    /*入库查询报表导出*/
    @RequiresPermissions("in:inRecord:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BillInDetailStreamVo billInDetailStreamVo) {
        List<BillInDetailStreamVo> list = billInRecordService.findStreamList(billInDetailStreamVo);
        ExcelUtil<BillInDetailStreamVo> util = new ExcelUtil<BillInDetailStreamVo>(BillInDetailStreamVo.class);
        return util.exportExcel(list, "入库记录");
    }
    /**
     * 导出列表
     */
    //@RequiresPermissions("in:billInMaster:partExcel")
    @PostMapping("/partExcel")
    @ResponseBody
    public AjaxResult partExcel(@RequestParam  String ids) {
        List<BillInRecordDto> byIds=new ArrayList();
        String[] split = ids.split(",");
        for (String s:split
        ) {
            BillInRecordCriteria b=new BillInRecordCriteria ();
            b.setBillInRecordId(Integer.parseInt(s));
            List<BillInRecordDto> list = billInRecordService.findList(b);
            for (BillInRecordDto bs:list
            ) {
                byIds.add(bs);
            }

        }
        // List<BillInSapDto> billInSapList = billInSapService.findBillInSapDtoList(billInSapCriteria);
        ExcelUtil<BillInRecordDto> util = new ExcelUtil<BillInRecordDto>(BillInRecordDto.class);
        return util.exportExcel(byIds, "入库单列表");
    }
}
